package query

import (
	"strings"

	"ssalarm/common/dto"
)

// 告警配置列表查询条件
type SysAlarmConfigQuery struct {
	dto.Pagination
	dto.OrderBy `search:"-"`
	dto.UserInfo
	Sn          string   `form:"sn" search:"type:contains;column:dev_sn;table:sys_alarm_config"`
	Key         string   `form:"key" search:"type:contains;column:key;table:sys_alarm_config"`
	Search      string   `form:"search" search:"type:likeOr;column:dev_sn,key,product_code;table:sys_alarm_config"`
	ProductId   string   `form:"productId" search:"type:exact;column:dev_product_id;table:sys_alarm_config"`
	ProductCode string   `form:"productCode" search:"type:contains;column:product_code;table:sys_alarm_config"`
	DeptId      string   `form:"deptId" search:"-"`
	AlarmLevel  *int     `form:"alarmLevel" search:"-"`
	AlarmType   string   `form:"alarmType" search:"-"`
	KeyName     string   `form:"keyName" search:"-"`
	LimitType   *int     `form:"limitType" search:"-"`
	ProdOrDev   string   `form:"prodOrDev" search:"-"`
	SnIn        []string `form:"snIn" search:"type:in;column:dev_sn;table:sys_alarm_config"`

	IsProduct     bool     `search:"type:notnullstr;column:product_code;table:sys_alarm_config"`
	IsDevice      bool     `search:"type:isNil;column:product_code;table:sys_alarm_config"`
	SnEq          string   `search:"type:exact;column:dev_sn;table:sys_alarm_config"`
	ProductCodeEq string   `search:"type:exact;column:product_code;table:sys_alarm_config"`
	KeyEq         string   `search:"type:exact;column:key;table:sys_alarm_config"`
	KeyIn         []string `search:"type:in;column:key;table:sys_alarm_config"`
	DevDeptIdIn   []string `search:"-"`
}

func (m *SysAlarmConfigQuery) GetNeedSearch() SysAlarmConfigQuery {
	if len(m.DeptId) == 0 {
		if m.UserId != "1" {
			m.DevDeptIdIn = m.DeptIdList
		}
	} else {
		m.DevDeptIdIn = strings.Split(strings.ReplaceAll(m.DeptId, " ", ""), ",")
	}
	if m.ProdOrDev == "product" {
		m.IsProduct = true
	} else if m.ProdOrDev == "device" {
		m.IsDevice = true
	}
	return *m
}

// 单个设备告警配置查询
type SysDevAlarmConfigQuery struct {
	dto.OrderBy `search:"-"`
	Sn          string `form:"sn"  search:"type:exact;column:dev_sn;table:sys_alarm_config"`
	Key         string `form:"key"  search:"type:exact;column:key;table:sys_alarm_config"`
	ProductId   string `form:"productId"  search:"type:exact;column:dev_product_id;table:sys_alarm_config"`
	ProductCode string `form:"productCode"  search:"type:exact;column:product_code;table:sys_alarm_config"`
}

func (m *SysDevAlarmConfigQuery) GetNeedSearch() SysDevAlarmConfigQuery {
	return *m
}
